﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jbe.TestSuite.Infrastructure.Interface;
using System.Windows.Media;
using System.ComponentModel;
using System.IO;
using Microsoft.Practices.Composite.Presentation.Commands;
using Jbe.TestSuite.RtfEditor.Domain;
using Jbe.Composite.Extensions;
using System.Windows;
using System.Reflection;
using System.Windows.Documents;
using Jbe.TestSuite.RtfEditor.Properties;

namespace Jbe.TestSuite.RtfEditor.Applications
{
    internal class RichTextEditorPresentationModel : PresentationModel<IRichTextEditorView>, IHeaderInfoProvider
    {
        private readonly RichTextDocument document;


        public RichTextEditorPresentationModel(IRichTextEditorView view, RichTextDocument document)
            : base(view)
        {
            this.document = document;
            PropertyChangedEventManager.AddListener(document, this, "");
        }


        public RichTextDocument Document { get { return document; } }

        public string HeaderInfo
        {
            get { return Path.GetFileName(document.FileName) ?? string.Format(Resources.DocumentTemplate, 1); }
        }

        public ImageSource HeaderIcon
        {
            get { return ResourceService.CreateBitmapSource(Resources.TextDoc.ToBitmap()); }
        }


        protected override void ReceiveWeakEvent(Type managerType, object sender, EventArgs e, ref bool handled)
        {
            base.ReceiveWeakEvent(managerType, sender, e, ref handled);

            if (managerType == typeof(PropertyChangedEventManager) && sender == document)
            {
                DocumentPropertyChanged(sender, (PropertyChangedEventArgs)e);
                handled = true;
            }
        }

        private void DocumentPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "FileName") { RaisePropertyChanged("HeaderInfo"); }
        }
    }
}
